package itdep.common.resolver;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Ivan Khalopik
 * @version $Revision: 4 $ $Date: 2010-06-10 13:13:16 +0000 (Thu, 10 Jun 2010) $
 */
public class CachedResolver<K, V> implements Resolver<K, V> {
	private final Map<K, V> cache = new HashMap<K, V>();
	private final Resolver<K, V> resolver;

	public CachedResolver(final Resolver<K, V> resolver) {
		this.resolver = resolver;
	}

	public V resolve(final K key) {
		if (!cache.containsKey(key)) {
			final V value = resolver.resolve(key);
			if (value != null) {
				cache.put(key, value);
			}
		}
		return cache.get(key);
	}
}
